package Five;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 长风无言
 */
public class ClientThread {

    private DataInputStream in;
    private DataOutputStream out;
    private static Socket s = null;

    Scanner sc = new Scanner(System.in);

    public Thread sendFile(Socket ss) {

        s = ss;

        try {
            out = new DataOutputStream(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //lamda表达式创建并启动线程
        Thread thread = new Thread(() -> {
            while (true) {
//                System.out.println("输入：");
                String str1 = sc.next();
                //检查输入是否是”file“单词
                if ("file".equals(str1)) {
                    try {
                        //是就进入循环并将”file“发送服务器告诉服务器开始接收文件
                        out.writeUTF(str1);
                        int i = 0;
                        File f = null;
                        System.out.println("请输入文件路径：");
                        String path = sc.next();
                       /* String[] arry1 = path.split("/");
                        for (String str : arry1) {
                            if (str != null) {
                                i++;
                            }
                        }*/
                        f = new File(path);
                        if (!f.exists()) {
                            System.out.println("文件不存在。");
                        }
                        System.out.println("文件长度：" + f.length());
                        //按照"文件名；文件长度"
                        out.writeUTF(f.getName()+";"+String.valueOf(f.length()));
                        //关联文件
                        FileInputStream in = new FileInputStream(f);
                        byte[] arry = new byte[8 * 1024];
                        int res = 0;
                        //将读入数据全部发送给服务器
                        while ((res = in.read(arry)) != -1) {
                            out.write(arry, 0, res);
                            out.flush();
                        }
                        System.out.println("文件发送成功");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else{
                    //如果输入不是"file"
                    try {
                        //直接把聊天消息发送给服务器
                        out.writeUTF(str1);
//                        System.out.println("发送完毕。");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        );
        return thread;
    }

    public Thread recevie (){

        try {
            in = new DataInputStream(s.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Thread thread1 = new Thread(()->{
            while(true){
                String str = null;
                try {
//                    System.out.println("开始接收");
                    //接收消息
                    str = in.readUTF();
//                    System.out.println("接收完毕");
                    String type = str.split(";")[0];
                    //检测是否发送的是file组合信息
                    if("file".equals(type)){
                        //拆分出的第二部分是文件名
                        String name = str.split(";")[1];
                        //第三部分是文件长度
                        int length =Integer.valueOf(str.split(";")[2]);
//                        System.out.println("我进来了");
                        File f = new File("d:/test3/" + name);
                        System.out.println("接收文件：" + f.getName());
                        //创建并关联本地文件
                        FileOutputStream fout = new FileOutputStream(f);
                        byte[] arry1 = new byte[8 * 1024];
                        int res = 0;
                        //写入文件
                        System.out.println("开始写入文件！");
                        while (true) {
                            res = in.read(arry1, 0, arry1.length);
                            fout.write(arry1, 0, res);
                            fout.flush();
                            if (f.length() == length) {
                                System.out.println("文件接受完毕");
                                break;
                            }
                        }
                    }else{
                        System.out.println("接收到的消息："+"\n"+str);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        );
        return thread1;
    }
}
